package com.duleme.servlet;

import com.duleme.common.EncodeUtils;
import com.duleme.common.JsonUtils;
import com.duleme.dao.DeliveryDao;
import com.duleme.entity.Delivery;
import com.duleme.entity.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(urlPatterns = "/addAddress")
public class AddNewAddressServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        EncodeUtils.setEncoding(req,resp);
        User user=(User)req.getSession().getAttribute("user");
        /*if(user==null)user=new User();
        user.setUserId(100020);*/
        int userId=user.getUserId();
        String country=req.getParameter("country");
        String province=req.getParameter("province");
        String city=req.getParameter("city");
        String county=req.getParameter("county");
        String street=req.getParameter("street");
        String detailedAddress=req.getParameter("address");
        String phoneNumber=req.getParameter("phoneNumber");
        String name=req.getParameter("name");
        Delivery delivery=new Delivery();
        delivery.setCountry(country);
        delivery.setProvince(province);
        delivery.setCity(city);
        delivery.setCounty(county);
        delivery.setStreet(street);
        delivery.setDetailedAddress(detailedAddress);
        delivery.setPhoneNumber(phoneNumber);
        delivery.setName(name);
        DeliveryDao dao=new DeliveryDao();
        if(dao.insert(delivery,userId)){
            resp.getWriter().print(JsonUtils.result2Json("1"));
        }else
            resp.getWriter().print(JsonUtils.result2Json("0"));
    }
}
